// 状态机练习

/**
 * 
 * 使用状态机 查询 abcabx
 * 
 * @param {*} source 
 * @returns 
 */
function find_abcabx(source) {
    let findIndex = -1;

    let stateFunction = find_a1;
    for(let index in source) {
        stateFunction = find_a1(source, index);
        if(stateFunction == findSuccess) {
            findIndex = index;
            break;
        } 
    }

    return findIndex;
}




function find_a1(source, index) {
    // console.log("find_a1 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "a") {
            return find_b1(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function find_b1(source, index) {
    // console.log("find_b1 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "b") {
            return find_c(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function find_c(source, index) {
    // console.log("find_c index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "c") {
            return find_a2(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function find_a2(source, index) {
    // console.log("find_a2 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "a") {
            return find_b2(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}


function find_b2(source, index) {
    // console.log("find_b2 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "b") {
            return find_x(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function find_x(source, index) {
    // console.log("find_x index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "x") {
            return findSuccess;
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function findEmtpy(source, index) {
    return -1;
}

function findSuccess(source, index) {
    return 0;
}

let result1 = find_abcabx("abcabx");
console.log(" result1:" + result1);
let result2 = find_abcabx("demo2021abcabxzxc");
console.log(" result2:" + result2);
let result3 = find_abcabx("demoabcabz12");
console.log(" result3:" + result3);


// 状态机 第二个练习 查询 abababx

function find_abababx(source) {
    let findIndex = -1;

    let stateFunction = find_a1;
    for(let index in source) {
        stateFunction = findE2_a1(source, index);
        if(stateFunction == findSuccess) {
            findIndex = index;
            break;
        } 
    }

    return findIndex;
}

function findE2_a1(source, index) {
    // console.log("findE2_a1 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "a") {
            return findE2_b1(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function findE2_b1(source, index) {
    // console.log("findE2_b1 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "b") {
            return findE2_a2(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function findE2_a2(source, index) {
    // console.log("findE2_a2 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "a") {
            return findE2_b2(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function findE2_b2(source, index) {
    // console.log("findE2_b2 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "b") {
            return findE2_a3(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function findE2_a3(source, index) {
    // console.log("findE2_a3 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "a") {
            return findE2_b3(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function findE2_b3(source, index) {
    // console.log("findE2_b3 index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "b") {
            return findE2_x(source, parseInt(index) + 1);
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

function findE2_x(source, index) {
    // console.log("findE2_x index:" + index);
    if(index <= source.length) {
        let c = source.charAt(index);
        if(c == "x") {
            return findSuccess;
        } else {
            return findEmtpy;
        }
    } else {
        return findEmtpy;
    }
}

let resultE2_1 = find_abababx("abababx");
console.log(" resultE2_1:" + resultE2_1);
let resultE2_2 = find_abababx("demo2021abababx123");
console.log(" resultE2_2:" + resultE2_2);
let resultE2_3 = find_abababx("demoabababz123");
console.log(" resultE2_3:" + resultE2_3);


// 状态机 KMP (选做)

